"""
 -*- coding: utf-8-*-
 @File: student_management.py
 @Author: chen
 @time: 2023/07/22/ 8:48
"""
from dataclasses import dataclass

from hogwartsHomework.homework_python.management.my_error import StudentError


@dataclass
class Student:
    id: int
    name: str
    sex: str

    def __repr__(self):
        return f"学号：{self.id}，\t姓名：{self.name}，\t性别：{self.sex}"


class StudentManagement:
    def __init__(self):
        """初始化学生列表"""
        self.student_list = []

    def is_exist(self, stu_id):
        """通过学号判断学生是否已存在"""
        for stu in self.student_list:
            # 如果已存在，则返回该学生
            if stu.id == stu_id:
                return stu
        else:
            return False

    def find_student(self, stu_id):
        """通过学号判断学生是否已存在"""
        stu = self.is_exist(stu_id)
        if stu:
            return stu
        else:
            # return False
            from hogwartsHomework.homework_python.management.my_error import StudentNotFoundError
            raise StudentNotFoundError(stu_id)

    def add_student(self, stu_id, stu_name, stu_sex):
        """创建添加学生的方法"""
        # 如果学生不存在，则可以添加学生
        if not self.is_exist(stu_id):
            stu = Student(stu_id, stu_name, stu_sex)
            self.student_list.append(stu)
            # print(stu)
        else:
            # print("学生已存在")
            from hogwartsHomework.homework_python.management.my_error import StudentIsExistError
            raise StudentIsExistError(stu_id)

    def main(self):
        """添加三个学生"""
        self.add_student(1001, "张三", "男")
        self.add_student(1002, "莉丝", "女")
        self.add_student(1003, "王武", "男")

    def delete_student(self, stu_id):
        """根据学号删除学生"""
        # 如果学生已存在，则可以删除学生
        if self.is_exist(stu_id):
            self.student_list.remove(self.find_student(stu_id))
        else:
            # return False
            from hogwartsHomework.homework_python.management.my_error import StudentNotFoundError
            raise StudentNotFoundError(stu_id)

    def show_all_student(self):
        """查询所有学员信息"""
        for stu in self.student_list:
            stu = Student(stu.id, stu.name, stu.sex)
            print(stu)

    def find(self):
        while True:
            s_id = input("请输入要查找的学员学号：")
            if s_id.isdigit():
                s_id = int(s_id)
                try:
                    stu = self.find_student(s_id)
                    print("学员信息获取成功，该学员的信息为：")
                    print(stu)
                    break
                except StudentError as err:
                    print(err)
            else:
                print("输入类型有误，请重新输入")

    def add(self):
        while True:
            s_id = input("请输入学员学号：")
            if s_id.isdigit():
                s_id = int(s_id)
                s_name = input("请输入学员姓名：")
                s_sex = input("请输入学员性别：")
                try:
                    self.add_student(s_id, s_name, s_sex)
                    print("添加成功，添加的学员信息为：")
                    print(self.find_student(s_id))
                    break
                except StudentError as err:
                    print(err)
            else:
                print("输入类型有误，请输入数字")

    def delete(self):
        while True:
            s_id = input("请输入要删除的学员学号：")
            if s_id.isdigit():
                s_id = int(s_id)
                try:
                    stu = self.find_student(s_id)
                    self.delete_student(s_id)
                    print(f"删除成功，该学员信息为：{stu}")
                    print("删除后的学员信息为：")
                    self.show_all_student()
                    break
                except StudentError as err:
                    print(err)
            else:
                print("输入类型有误，请输入数字")

    def all(self):
        self.show_all_student()


if __name__ == '__main__':
    mgt = StudentManagement()
    mgt.main()
    # print(mgt.student_list)
    mgt.show_all_student()
